﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// DeviceBindWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceBindWindow : Window
    {
        public DeviceBindWindow()
        {
            InitializeComponent();
        }



         
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            setlang();
             
            getConfigs();
        }

        private void setlang()
        {
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                this.Title = "ئۈسكۈنە قوشۇش";
                HintAssist.SetHint(combo0, "پىرىنتېر ئىسمى");
                HintAssist.SetHint(text1, "تاماق ئىسمى");

                btn_cancel.Content = "قالدۇرۇش";
                btn_ok.Content = "ساقلاش";

                col_name.Header = "ئىسمى";
                col_printer_name.Header = "پىرىنتېر ئىسمى";
                col_food_type.Header = "تاماق ئىسمى";


                btn_del.Content = "ئۆچۈرۈش";
                btn_choose.Content = "تاللاش";
            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                this.Title = "设备添加";
                col_name.Header = "设备名称";
                HintAssist.SetHint(combo0, "打印机名称");
                HintAssist.SetHint(text1, "食品名称");

                btn_cancel.Content = "取消";
                btn_ok.Content = "确定";
                col_printer_name.Header = "打印机名称";
                col_food_type.Header = "饭菜名称";
                btn_del.Content = "删除";
                btn_choose.Content = "选择";
            }
        }

        private void getConfigs()
        {
          //// combo1.Items.Clear();
          //
          // //print type
          // DataTable dt1 = sqlite.getData("select * from other_config  where type='food_type' and lang='" + lang + "'");
          //
          // for (int i = 0; i < dt1.Rows.Count; i++)
          // {
          //     string idd = dt1.Rows[i]["id"].ToString();
          //     string value = dt1.Rows[i]["value"].ToString();
          //     ComboBoxItem item = new ComboBoxItem();
          //     item.Content = value;
          //     item.Tag = idd;
          //     combo1.Items.Add(item);
          //
          // }

            //printers
            DataTable dt2 = sqlite.getData("select id,name from printer where lang='" + lang + "' and status='1' and print_type in ('2','4') ");

            for (int i = 0; i < dt2.Rows.Count; i++)
            {
                string idd = dt2.Rows[i]["id"].ToString();
                string value = dt2.Rows[i]["name"].ToString();
                ComboBoxItem item = new ComboBoxItem();
                item.Content = value;
                item.Tag = idd;
                combo0.Items.Add(item);

            }



            loadGrid();
          


           

        }

        
        private void loadGrid()
        {

            //printer_binding
            DataTable dt3 = sqlite.getData("select * from printer_binding_view where lang='" + lang + "'");

            dgv2.ItemsSource = dt3.DefaultView;



        }


        SQLiteHelper sqlite = new SQLiteHelper();


        public string lang { get; set; }

        DataTable data = new DataTable();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;
            switch (name)
            {
                case "btn_cancel":
                    this.Close();
                    break;
                case "btn_choose":
                    FoodsWindow foods = new FoodsWindow();
                    foods.Owner = this;
                    foods.lang = lang;
                    foods.ShowDialog();
                    if (foods.data.Rows.Count > 0)
                    {
                        data=foods.data;
                        string txt = "";
                        for(int i = 0; i < data.Rows.Count; i++)
                        {
                            txt+=data.Rows[i]["name"].ToString()+",";
                            
                        }
                        text1.Text = txt;
                        

                    }
                    break;
                case "btn_ok":

                    if (combo0.SelectedItem == null)
                    {
                        return;
                    }

                    if (data.Rows.Count == 0)
                    {
                        return;
                    }

                    ComboBoxItem item0 = (ComboBoxItem)combo0.SelectedItem;
                    string printer_id = item0.Tag.ToString();

                    // ComboBoxItem item1 = (ComboBoxItem)combo1.SelectedItem;
                    //  string food_type = item1.Tag.ToString();
                    string food_type = "";
                    for (int i = 0; i < data.Rows.Count; i++)
                    {
                        food_type=data.Rows[i]["id"].ToString();
                        add(printer_id, food_type);

                    }
                     

                    string msg = "";
                    if (lang.Equals("ug"))
                    {
                        msg = "ئوڭۇشلۇق بولدى";
                    }
                    else
                    {
                        msg = "成功";
                    }

                    MessageBox.Show(msg);

                    loadGrid();

                    //this.Close();

                    break;
                case "btn_del":
                    if (dgv2.SelectedItem != null)
                    {
                        DataRowView row = (DataRowView)dgv2.SelectedItem;
                        string id=row.Row.ItemArray[0].ToString();
                        sqlite.execute("delete from printer_binding where id='" + id + "'");
                        MessageBox.Show("ok");
                        loadGrid();

                    }

                    break;

            }
        }

        private void add(string printer_id, string food_type)
        {

             
            string sql = "select * from printer_binding where printer_id='" + printer_id + "' and food_type='" + food_type + "' and lang='" + lang + "'";
            DataTable dt = sqlite.getData(sql);
            if (dt.Rows.Count == 0)
            {
                sql = "insert into printer_binding (printer_id,food_type,lang) " +
                    " values ('" + printer_id +"','" + food_type + "','" + lang + "')";
                sqlite.insert(sql);
            }
        }

       

        
    }
}
